
const fs = require('fs');
const { request } = require('https');
//axios 可以替代 request 吗？
const request = require('request');

module.exports.downloadFile = async (src, localFilePath, data)=>{
    try{
        let ws = fs.createWriteStream(localFilePath);
        return new Promise((resolve,reject)=>{
            ws.on('finish', ()=>{
                resolve(localFilePath);
            });
            if(data){
                request({
                    method: 'POST',
                    uri: src,
                    json: true,
                    body: data
                }).pipe(ws);
            }else{
                request(src).pipe(ws);
            }
        });
    }catch(e){
        logger.error('wxdownload error', e);
    }
}

// nodejs 发送何接收请求